package com.microcm.jstl;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.microcm.common.CUtils;
import com.microcm.common.SystemToolkit;
import com.microcmframework.core.context.AppContext;

/**
 * 配置项输出标签
 * 
 * @author kansum
 */
public class ConfigTag extends TagSupport
{
	private static final long serialVersionUID = 1L;

	private String type;

	private String key;

	private String def;

	public void setKey(String key)
	{
		this.key = key;
	}

	public void setDef(String def)
	{
		this.def = def;
	}

	public void setType(String type)
	{
		this.type = type;
	}

	@Override
	public int doStartTag() throws JspException
	{
		JspWriter out = this.pageContext.getOut();
		String val = null;
		if (CUtils.isNotEmpty(type) && "prop".equals(type))
			val = AppContext.getInstance().getSysPropValue(key);
		else
			val = SystemToolkit.getSystemConfigValue(key, this.def);

		try
		{
			out.print(val);
		}
		catch (IOException e)
		{
			throw new JspException(e);
		}
		return SKIP_BODY;
	}
}
